package ru.tulupov.producerconsumerpattern;

public abstract class Consumer<T> extends Thread {
	private Storage<T> storage;

	public Consumer(Storage<T> storage) {
		super("Consumer");
		this.storage = storage;
	}

	@Override
	public void run() {
		onStart();
		while (true) {
			try {
				T element = storage.get();
				onReceive(element);
			} catch (InterruptedException e) {
				T element = null;
				while ((element = storage.removeLastElement()) != null) {
					onReceive(element);
				}

				break;
			}
		}

		onStop();
	}

	protected abstract void onReceive(T element);

	protected void onStop() {

	}

	protected void onStart() {

	}
}
